import Vue from 'vue';
import {toastForward} from "./index";
import * as Fiber from 'fiber';
// 转发协同
const transmitColFromGrid = function (selectedRows, resetTableData, listFrom, isSecretLevelEnable) {
  // 需要判断是否只勾选了一项，并给出相应的提示
  if (selectedRows.length === 0) {
    //请选择要转发的协同
    Fiber.message.warning($.i18n('collaboration.grid.alert.transmitCol'));
    return;
  } else if (selectedRows.length > 1 && isSecretLevelEnable) {
    Fiber.message.warning($.i18n('collabration.secret.transfer.num.limit'));
    return;
  } else if (selectedRows.length > 20) {
    //只能选择20项协同进行转发
    Fiber.message.warning($.i18n('collaboration.grid.alert.transmitColOnly20'));
    return;
  }

  console.log("$.ctp.trigger", "beforeTransmitCol", selectedRows);
  const sendDevelop = $.ctp.trigger('beforeTransmitCol', selectedRows);
  if (!sendDevelop) {
    return;
  }

  for (var i = 0; i < selectedRows.length; i++) {
    var selectedObj = selectedRows[i];
    var app = selectedObj.applicationCategoryKey || selectedObj.app;

    if (app == 19 || app == 20 || app == 21) {
      //公文不允许转发协同
      Fiber.message.warning($.i18n('collaboration.grid.alert.DocumentNotForwardCol'));
      return;
    } else if (app == '32') {
      //信息报送不允许转发协同
      Fiber.message.warning($.i18n('collaboration.grid.alert.InfoNotForwardCol'));
      return;
    } else if (app == 81) {
      //模板审批不允许转发协同
      Fiber.message.warning($.i18n('collaboration.grid.alert.InfoNotTemplateApproveColl'));
      return;
    } else if (app == 4) {
      Fiber.message.warning($.i18n('govdoc.grid.alert.DocumentNotForwardCol'));
      return;
    }
  }

  var data = []; //var cm = new colManager();

  for (var i = 0; i < selectedRows.length; i++) {
    var summaryId, affairId, isSuperNode;

    if (selectedRows[i].summaryId && selectedRows[i].affairId) {
      summaryId = selectedRows[i].summaryId;
      affairId = selectedRows[i].affairId;
      isSuperNode = selectedRows[i].superNode;
    } else {
      //首页待办更多是Affair对象
      summaryId = selectedRows[i].objectId;
      affairId = selectedRows[i].id;
    }

    if (listFrom && listFrom == "listWaitSend") {
      if (selectedRows[i].templeteId) {
        //var r = cm.checkTemplateCanUse(selectedRows[i].templeteId);
        var r = callBackendMethod("colManager", "isTemplateDeleted", selectedRows[i].templeteId);

        if (r == "1") {
          //被删除
          Fiber.message.warning($.i18n("template.coll.not.ues.js", selectedRows[i].subject));
          return;
        }
      }
    }

    if (isSuperNode === true) {
      Fiber.message.warning($.i18n("workflow.supernode.msg.not_transmit.js"));
      return;
    }

    data[i] = {
      "summaryId": summaryId,
      "affairId": affairId
    };
  }

  transmitColById(data, resetTableData);
};
const transmitColById = function (data, resetTableData) {
  var checkPermissionData = "";
  var doForwardData = "";

  for (var i = 0; i < data.length; i++) {
    doForwardData += data[i]["summaryId"] + "_" + data[i]["affairId"] + ",";
    checkPermissionData += data[i]["affairId"] + ",";
  }

  var r = callBackendMethod("colManager", "checkForwardPermission", checkPermissionData);

  if (r && r instanceof Array && r.length > 0) {
    //以下协同不能转发，请重新选择
    toastForward.call(Vue, r);
    return;
  }

  var dialog = $.dialog({
    id: "showForwardDialog",
    height: "400",
    width: "550",
    url: _ctxPath + "/collaboration/collaboration.do?method=showForward&data=" + doForwardData,
    title: $.i18n('common.toolbar.transmit.col.label'),
    targetWindow: getCtpTop(),
    isClear: false,
    transParams: {
      commentContent: ""
    },
    buttons: [{
      id: "okButton",
      text: $.i18n("common.button.ok.label"),
      btnType: 1,
      handler: function handler() {
        dialog.getReturnValue();
      },
      OKFN: function OKFN() {
        dialog.close();
        resetTableData();
      }
    }, {
      id: "cancelButton",
      text: $.i18n("common.button.cancel.label"),
      handler: function handler() {
        dialog.close();
      }
    }]
  });
}; //ajax判断事项是否可用。
// 转发邮件
const transmitMail = function (selectedRows, resetTableData) {
  const emailShow = window._hasPlugin('webmail');
  if ("true" == emailShow || emailShow == true) {
    //需要判断是否只勾选了一项，并给出相应的提示
    if (selectedRows.length === 0) {
      //请选择要转发的协同!
      Fiber.message.warning($.i18n('collaboration.grid.alert.transmitCol'));
      return;
    } else if (selectedRows.length > 1) {
      //只能选择一项协同进行转发
      Fiber.message.warning($.i18n('collaboration.grid.alert.transmitColOnlyOne'));
      return;
    }

    console.log("$.ctp.trigger", "beforeTransmitMail", selectedRows);
    const sendDevelop = $.ctp.trigger('beforeTransmitMail', selectedRows);
    if (!sendDevelop) {
      return;
    }

    for (var i = 0; i < selectedRows.length; i++) {
      var selectedObj = selectedRows[i];
      var app = selectedObj.applicationCategoryKey || selectedObj.app;
      if (app == "19" || app == "20" || app == "21" || app == "4") {
        //公文不允许转发协同
        Fiber.message.warning($.i18n('collaboration.grid.alert.DocumentNotForwardEmail'));
        return;
      } else if (app == '32') {
        //信息报送不允许转发邮件
        Fiber.message.warning($.i18n('collaboration.grid.alert.InfoNotForwardEmail'));
        return;
      } else if (app == '81') {
        //协同模板不允许转发邮件
        Fiber.message.warning($.i18n('collaboration.grid.alert.InfoNotTemplateApproveEmail'));
        return;
      }
    }
    var summaryId, affairId, isSuperNode;
    if (selectedRows[0].summaryId && selectedRows[0].affairId) {
      summaryId = selectedRows[0].summaryId;
      affairId = selectedRows[0].affairId;
      isSuperNode = selectedRows[0].superNode;
    } else {
      //首页待办更多是Affair对象
      summaryId = selectedRows[0].objectId;
      affairId = selectedRows[0].id;
    } //TODO 陈超 transmitMail
    var canTrans = callBackendMethod("secretAjaxManager", "canTransSecretLevel", summaryId);
    if (canTrans == false) {
      Fiber.message.warning($.i18n('collaboration.grid.alert.noTransSecretData'));
      return;
    } //var cm = new colManager();
    // var r = cm.checkForwardPermission(summaryId + "_" + affairId);
    var r = callBackendMethod("colManager", "checkForwardPermission", affairId);
    if (r && r instanceof Array && r.length > 0) {
      //以下协同不能转发，请重新选择
      toastForward.call(Vue, r);
      return;
    }
    if (isSuperNode === true) {
      Fiber.message.warning($.i18n("workflow.supernode.msg.not_transmit.js"));
      return;
    }

    var params = new Object();
    params.title = $.i18n('common.toolbar.transmit.mail.label');

    if (getCtpTop() && getCtpTop().showMenu) {
      getCtpTop().showMenu(_ctxPath + "/collaboration/collaboration.do?method=forwordMail&summaryId=" + summaryId + "&affairId=" + affairId, null, null, null, params);
    } else {
      showMenu(_ctxPath + "/collaboration/collaboration.do?method=forwordMail&summaryId=" + summaryId + "&affairId=" + affairId, null, null, null, params);
    }
  }
};
export {
  transmitColFromGrid,
  transmitMail
};
